﻿using System;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using System.Net.Sockets;
using RedCorona.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
namespace Levi.Finance.Client
{
	public class Query : Silmoon.MySilmoon.IRunningAble, IDisposable
	{
		ClientInfo client;
		Socket sock;
		ABC g;
		public Query(ABC _g)
		{
			this.g = _g;
		}

		public void startSocket()
		{
			sock = Sockets.CreateTCPSocket("127.0.0.1", 2688);
			client = new ClientInfo(sock, false); // Don't start receiving yet
			//client.OnRead += new ConnectionRead (ReadData);
			//client.Delimiter = '\n'.ToString();  // this is the default, shown for illustration
			client.EncryptionType = EncryptionType.ServerRSAClientKey;
			client.MessageType = MessageType.CodeAndLength;
			client.OnReadMessage += new ConnectionReadMessage(ReadMessage);
			client.BeginReceive();
		}		

		void ReadMessage(ClientInfo ci, uint co, byte[] by, int length)
		{
			Console.WriteLine("Received from " + length + " byte!");
			if (co == 2882400001)
			{
				try
				{
					byte[] msg = new byte[by[4]];
					Array.Copy(by, 5, msg, 0, by[4]);
					string text = Encoding.Default.GetString(msg);
					Result result = new Result();
					result = JsonConvert.DeserializeObject<Result>(text);
					g.win.AlertMsg(result.info);
					if (result.status == 1)
					{
						switch(result.opt)
						{
							case 1:
								g.win.Hide();
								g.list.Show();
								g.GetFinanceList();
								break;
							case 2:
								ArrayList array = new ArrayList();
								array = (ArrayList)result.data;
								g.list.FinanceTree(array);
								break;
						}
					}
				}
				catch (Exception e)
				{
					e.ToString();
				}
			}
		}

		public bool SendMessage(string msg)
		{
			//byte[] bytes = Encoding.Default.GetBytes(msg);
			client.SendMessage(0xABCDEF01, Encoding.Default.GetBytes(msg), ParameterType.Int);
			return true;
		}
		
		#region IRunningAble 成员

        public bool Resume()
        {
            return false;
        }
        Silmoon.MySilmoon.RunningState _runningState = Silmoon.MySilmoon.RunningState.Stopped;
        public Silmoon.MySilmoon.RunningState RunningState
        {
            get { return _runningState; }
            private set { _runningState = value; }
        }

        public bool Start()
        {
            if (RunningState == Silmoon.MySilmoon.RunningState.Running) return false;
            RunningState = Silmoon.MySilmoon.RunningState.Running;
            startSocket();
            return true;
        }

        public void StartA()
        {
            Start();
        }

        public bool Stop()
        {
            if (RunningState == Silmoon.MySilmoon.RunningState.Stopped) return false;
            RunningState = Silmoon.MySilmoon.RunningState.Stopped;
            //Close();
            return true;
        }

        public bool Suspend()
        {
            return false;
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Stop();
            sock = null;
        }

        #endregion
	}
}

